<?php

/**
 * Institucion form base class.
 *
 * @method Institucion getObject() Returns the current form's model object
 *
 * @package    BCWeb
 * @subpackage form
 * @author     Jhonny Fuentes
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseInstitucionForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                           => new sfWidgetFormInputHidden(),
      'name'                         => new sfWidgetFormInputText(),
      'titulo'                       => new sfWidgetFormInputText(),
      'url'                          => new sfWidgetFormInputText(),
      'email'                        => new sfWidgetFormInputText(),
      'email_turno'                  => new sfWidgetFormInputText(),
      'email_incripcion'             => new sfWidgetFormInputText(),
      'email_consulta'               => new sfWidgetFormInputText(),
      'urlFotoLogo'                  => new sfWidgetFormInputText(),
      'prioridad'                    => new sfWidgetFormInputText(),
      'destacado'                    => new sfWidgetFormInputText(),
      'visitados'                    => new sfWidgetFormInputText(),
      'publicado'                    => new sfWidgetFormInputCheckbox(),
      'capturado'                    => new sfWidgetFormInputCheckbox(),
      'detalle_id'                   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Detalle'), 'add_empty' => true)),
      'tipo_id'                      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Tipo'), 'add_empty' => true)),
      'ideologia_id'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Ideologia'), 'add_empty' => true)),
      'sexo_id'                      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Sexo'), 'add_empty' => true)),
      'internado_id'                 => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Internado'), 'add_empty' => true)),
      'residencia_id'                => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Residencia'), 'add_empty' => true)),
      'internacional_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Internacional'), 'add_empty' => true)),
      'niveles_list'                 => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Nivel')),
      'jornadas_list'                => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Jornada')),
      'idiomas_list'                 => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Idioma')),
      'deporte_list'                 => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Deporte')),
      'certificacion_list'           => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Certificacion')),
      'tipo_pago_list'               => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'TipoPago')),
      'intercambio_estudiantil_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'IntercambioEstudiantil')),
      'sede_list'                    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Sede')),
      'facultad_instancia_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'FacultadInstancia')),
      'curso_instancia_list'         => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CursoInstancia')),
    ));

    $this->setValidators(array(
      'id'                           => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'name'                         => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'titulo'                       => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'url'                          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email'                        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email_turno'                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email_incripcion'             => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email_consulta'               => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'urlFotoLogo'                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'prioridad'                    => new sfValidatorInteger(array('required' => false)),
      'destacado'                    => new sfValidatorInteger(array('required' => false)),
      'visitados'                    => new sfValidatorInteger(array('required' => false)),
      'publicado'                    => new sfValidatorBoolean(array('required' => false)),
      'capturado'                    => new sfValidatorBoolean(array('required' => false)),
      'detalle_id'                   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Detalle'), 'required' => false)),
      'tipo_id'                      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Tipo'), 'required' => false)),
      'ideologia_id'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Ideologia'), 'required' => false)),
      'sexo_id'                      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Sexo'), 'required' => false)),
      'internado_id'                 => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Internado'), 'required' => false)),
      'residencia_id'                => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Residencia'), 'required' => false)),
      'internacional_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Internacional'), 'required' => false)),
      'niveles_list'                 => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Nivel', 'required' => false)),
      'jornadas_list'                => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Jornada', 'required' => false)),
      'idiomas_list'                 => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Idioma', 'required' => false)),
      'deporte_list'                 => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Deporte', 'required' => false)),
      'certificacion_list'           => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Certificacion', 'required' => false)),
      'tipo_pago_list'               => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'TipoPago', 'required' => false)),
      'intercambio_estudiantil_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'IntercambioEstudiantil', 'required' => false)),
      'sede_list'                    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Sede', 'required' => false)),
      'facultad_instancia_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'FacultadInstancia', 'required' => false)),
      'curso_instancia_list'         => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'CursoInstancia', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('institucion[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Institucion';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['niveles_list']))
    {
      $this->setDefault('niveles_list', $this->object->Niveles->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['jornadas_list']))
    {
      $this->setDefault('jornadas_list', $this->object->Jornadas->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['idiomas_list']))
    {
      $this->setDefault('idiomas_list', $this->object->Idiomas->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['deporte_list']))
    {
      $this->setDefault('deporte_list', $this->object->Deporte->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['certificacion_list']))
    {
      $this->setDefault('certificacion_list', $this->object->Certificacion->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tipo_pago_list']))
    {
      $this->setDefault('tipo_pago_list', $this->object->TipoPago->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['intercambio_estudiantil_list']))
    {
      $this->setDefault('intercambio_estudiantil_list', $this->object->IntercambioEstudiantil->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['sede_list']))
    {
      $this->setDefault('sede_list', $this->object->Sede->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['facultad_instancia_list']))
    {
      $this->setDefault('facultad_instancia_list', $this->object->FacultadInstancia->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['curso_instancia_list']))
    {
      $this->setDefault('curso_instancia_list', $this->object->CursoInstancia->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveNivelesList($con);
    $this->saveJornadasList($con);
    $this->saveIdiomasList($con);
    $this->saveDeporteList($con);
    $this->saveCertificacionList($con);
    $this->saveTipoPagoList($con);
    $this->saveIntercambioEstudiantilList($con);
    $this->saveSedeList($con);
    $this->saveFacultadInstanciaList($con);
    $this->saveCursoInstanciaList($con);

    parent::doSave($con);
  }

  public function saveNivelesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['niveles_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Niveles->getPrimaryKeys();
    $values = $this->getValue('niveles_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Niveles', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Niveles', array_values($link));
    }
  }

  public function saveJornadasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['jornadas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Jornadas->getPrimaryKeys();
    $values = $this->getValue('jornadas_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Jornadas', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Jornadas', array_values($link));
    }
  }

  public function saveIdiomasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['idiomas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Idiomas->getPrimaryKeys();
    $values = $this->getValue('idiomas_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Idiomas', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Idiomas', array_values($link));
    }
  }

  public function saveDeporteList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['deporte_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Deporte->getPrimaryKeys();
    $values = $this->getValue('deporte_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Deporte', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Deporte', array_values($link));
    }
  }

  public function saveCertificacionList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['certificacion_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Certificacion->getPrimaryKeys();
    $values = $this->getValue('certificacion_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Certificacion', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Certificacion', array_values($link));
    }
  }

  public function saveTipoPagoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tipo_pago_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->TipoPago->getPrimaryKeys();
    $values = $this->getValue('tipo_pago_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('TipoPago', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('TipoPago', array_values($link));
    }
  }

  public function saveIntercambioEstudiantilList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['intercambio_estudiantil_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->IntercambioEstudiantil->getPrimaryKeys();
    $values = $this->getValue('intercambio_estudiantil_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('IntercambioEstudiantil', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('IntercambioEstudiantil', array_values($link));
    }
  }

  public function saveSedeList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['sede_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Sede->getPrimaryKeys();
    $values = $this->getValue('sede_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Sede', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Sede', array_values($link));
    }
  }

  public function saveFacultadInstanciaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['facultad_instancia_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->FacultadInstancia->getPrimaryKeys();
    $values = $this->getValue('facultad_instancia_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('FacultadInstancia', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('FacultadInstancia', array_values($link));
    }
  }

  public function saveCursoInstanciaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['curso_instancia_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CursoInstancia->getPrimaryKeys();
    $values = $this->getValue('curso_instancia_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CursoInstancia', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CursoInstancia', array_values($link));
    }
  }

}
